/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package googleSearch;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.List;
import com.google.gson.Gson;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
 *
 * @author Asep Sucipto
 */
public class GoogleSearch {

    /**
     * @param args the command line arguments
     */
    public static ArrayList<String> SearchGoogle(String query) throws IOException {
        // TODO code application logic here 
        //n=jumlah hasil yang diinginkan
        int n=10;
	ArrayList hasil= new ArrayList<String>();	
        for (int j = 0; j < n; j = j + 4) {
          //  http://ajax.googleapis.com/ajax/services/search/web?v=1.0&start=4&q=
        String address = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&start="+j+"&q=";
		//String query = "bandung";
		String charset = "UTF-8";
                
 
		URL url;
        try {
            try {
                url = new URL(address + URLEncoder.encode(query, charset));
                Reader reader = new InputStreamReader(url.openStream(), charset);
		GoogleResults results = new Gson().fromJson(reader, GoogleResults.class);
		int total = results.getResponseData().getResults().size();
		System.out.println("total: "+total);
 
		// Show title and URL of each results
		for(int i=0; i<=total-1; i++){
			System.out.println("Title: " + results.getResponseData().getResults().get(i).getTitle());
			System.out.println("URL: " + results.getResponseData().getResults().get(i).getUrl() + "\n");
                        hasil.add(results.getResponseData().getResults().get(i).getUrl().toString());
 
		}
                
            } catch (UnsupportedEncodingException ex) {
                Logger.getLogger(GoogleSearch.class.getName()).log(Level.SEVERE, null, ex);
            }
            
        } catch (MalformedURLException ex) {
            Logger.getLogger(GoogleSearch.class.getName()).log(Level.SEVERE, null, ex);
        }
        }
        return hasil;
	}
    
}
 
 
class GoogleResults{
 
    private ResponseData responseData;
    public ResponseData getResponseData() { return responseData; }
    public void setResponseData(ResponseData responseData) { this.responseData = responseData; }
    public String toString() { return "ResponseData[" + responseData + "]"; }
 
    static class ResponseData {
        private List<Result> results;
        public List<Result> getResults() { return results; }
        public void setResults(List<Result> results) { this.results = results; }
        public String toString() { return "Results[" + results + "]"; }
    }
 
    static class Result {
        private String url;
        private String title;
        public String getUrl() { return url; }
        public String getTitle() { return title; }
        public void setUrl(String url) { this.url = url; }
        public void setTitle(String title) { this.title = title; }
        public String toString() { return "Result[url:" + url +",title:" + title + "]"; }
    }
}
  
